import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:test/providers/user_token_provider.dart';

// 创建一个自定义拦截器类
class TokenInterceptor extends Interceptor {
  final WidgetRef ref;

  TokenInterceptor(this.ref);

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // 从 Riverpod 中获取用户 token
    final token = ref.read(userTokenProvider);
    if (token != null) {
      // 如果 token 存在，将其添加到请求头中
      options.headers['Authorization'] = token;
    }
    super.onRequest(options, handler);
  }
}
